package com.gopay.cron.web.controller;

import com.gopay.cron.service.trans.ExportExcelService;
import com.gopay.cron.thread.TransRecordThread;
import com.gopay.scheduler.service.AutoGenTrade.AutoGenTradeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * @author qld
 *         手动生成某一天的成功交易记录
 *         输入的地址格式
 *         http://localhost:8080/此项目名称/index.shtml?date=想要生成的日期(yyyy-MM-dd)
 */

@Controller
public class TransRecordController {

    @Resource(name = "autoGenTradeService")
    private AutoGenTradeService autoGenTradeService;

    private @Value("#{webProperties['transDownload_downPath']}")
    String downPath;
    @Resource(name = "exportExcelService")
    private ExportExcelService exportExcelService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");



    @RequestMapping("/index")
    public void index() throws Exception {
        String date = getRequest().getParameter("date");
        /*** 加一天*/
        Date dd = simpleDateFormat.parse(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dd);
        calendar.add(Calendar.DAY_OF_MONTH, 1);//加一天
        //System.out.println("增加一天之后：" + df.format(calendar.getTime()));
        logger.info("-------开始手动生成交易记录-------"+date);

        new Thread(new TransRecordThread(exportExcelService,date, simpleDateFormat.format(calendar.getTime()))).start();

    }

    protected HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    }

}
